﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using Sector7.Restaurant.CoreType.Enums;
using Sector7.Restaurant.Modules.REST.LocalService;
using Sector7.Restaurant.Modules.REST.Utility;
using Sector7.Framework.Enums;
using Sector7.Restaurant.Utility;
using Sector7.Restaurant.Biz;
using Sector7.Framework.Utility;
using Sector7.Framework.Core;

namespace Sector7.Restaurant.Modules.REST.Windows
{
    /// <summary>
    /// AddMenuWindow.xaml 的交互逻辑
    /// </summary>
    public partial class AddMenuItemWindow : AddModeWindowBase
    {
        public AddMenuItemWindow(RS_MenuItemType selectedType, List<RS_Unit> units)
        {
            InitializeComponent();

            cbbUnit.ItemsSource = units;
            cbbUnit.SelectedIndex = 0;

            cbbType.ItemsSource = Aide.LocalClient.Invoke<List<RS_MetadataType>>(
                service => service.GetAllMetadataTypesForUsage((int)MetadataUsageType.MenuItem, true));
            cbbType.SelectedValue = selectedType.TypeId;

            lsbLabel.ItemsSource = Aide.LocalClient.Invoke<List<RS_MenuItemLabel>>(
                service => service.GetAllMenuItemLabels());
        }

        public RS_MenuItem MenuItem
        {
            get { return m_entity as RS_MenuItem; }
            set { m_entity = value; }
        }

        protected override void InitializeForCreating()
        {
            MenuItem = new RS_MenuItem();
            MenuItem.Description = string.Empty;
        }

        protected override void InitializeForUpdating()
        {
            cbbUnit.SelectedValue = MenuItem.UnitId;
            txbMenuItemId.Text = MenuItem.MenuItemId;

            btnContinue.Visibility = Visibility.Hidden;
        }

        protected override void AddData()
        {
            RS_Metadata selectedMetadata = dtgMenuItem.SelectedItem as RS_Metadata;

            if (selectedMetadata != null)
            {
                MenuItem.UnitId = (int)cbbUnit.SelectedValue;
                MenuItem.MetadataTypeId = (int)cbbType.SelectedValue;
                MenuItem.LabelIds = LabelIds;

                switch (m_crud)
                {
                    case CrudType.Create:
                        MenuItem.MetadataId = selectedMetadata.Id;

                        string menuItemId = Aide.LocalClient.Invoke<string>(
                            service => service.AddMenuItem(MenuItem));

                        if (menuItemId != null)
                        {
                            MenuItem = Aide.LocalClient.Invoke<RS_MenuItem>(
                                service => service.GetMenuItem(menuItemId));
                        }
                        break;
                    case CrudType.Update:
                        EntityDelta delta = ClientUtil.CreateDelta<EntityDelta>(typeof(RS_MenuItem));
                        delta.Conditions.Add("MenuItemId", MenuItem.MenuItemId);
                        delta.DeltaProperties.Add("UnitId", MenuItem.UnitId);
                        delta.DeltaProperties.Add("UnitPrice", MenuItem.UnitPrice);
                        delta.DeltaProperties.Add("Description", MenuItem.Description);
                        delta.DeltaProperties.Add("LabelIds", MenuItem.LabelIds);

                        Aide.LocalClient.Invoke<int>(
                            service => service.SetMenuItem(delta));
                        break;
                    default:
                        break;
                }

                base.AddData();
            }
        }

        private int LabelIds
        {
            get
            {
                int labelIds = 0;
                int temp = 0;

                foreach (CheckBox ckbLabel in FindAllCheckBoxForLabel())
                {
                    if (ckbLabel.IsChecked == true)
                    {
                        temp = 1 << Convert.ToInt32(ckbLabel.Tag);

                        labelIds |= temp;
                    }
                }

                return labelIds;
            }
            set
            {
                int labelIds = MenuItem.LabelIds;
                int temp = 0;

                foreach (CheckBox ckbLabel in FindAllCheckBoxForLabel())
                {
                    temp = 1 << Convert.ToInt32(ckbLabel.Tag);
                    ckbLabel.IsChecked = (labelIds & temp) == temp;
                }
            }
        }

        private List<CheckBox> FindAllCheckBoxForLabel()
        {
            List<CheckBox> checkBoxes = new List<CheckBox>();

            foreach (var item in lsbLabel.Items)
            {
                ListBoxItem listBoxItem = lsbLabel.ItemContainerGenerator.ContainerFromItem(item) as ListBoxItem;

                ContentPresenter contentPresenter = VisualTreeUtil.FindChild<ContentPresenter>(listBoxItem);

                DataTemplate dataTemplate = contentPresenter.ContentTemplate;

                checkBoxes.Add(dataTemplate.FindName("ckbLabel", contentPresenter) as CheckBox);
            }

            return checkBoxes;
        }

        private void cbbType_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            RS_MetadataType selectedType = cbbType.SelectedItem as RS_MetadataType;
            if (selectedType != null)
            {
                ClientUtil.LoadItemsSource(dtgMenuItem, selectedType.Metadatas);
            }
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            LabelIds = MenuItem.LabelIds;
        }
    }
}
